package org.example;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

public class HbaseConnectTest {
    public static void main(String[] args) {


        try {

            Configuration conf = HBaseConfiguration.create();
            conf.set("hbase.zookeeper.quorum", "ljc102,ljc103,ljc104");
            conf.set("hbase.zookeeper.property.clientPort", "2181");


            Connection connection = ConnectionFactory.createConnection(conf);
            Admin admin = connection.getAdmin();


            TableName[] tableNames = admin.listTableNames();
            for (TableName tableName : tableNames) {
                System.out.println(tableName.getNameAsString());
            }


            Table forumTable = connection.getTable(TableName.valueOf("lzb0117_forum"));
            Scan scan = new Scan();
            scan.setLimit(5);
            scan.setCaching(5);
            scan.setBatch(5);
            scan.setMaxResultSize(5);

            ResultScanner scanner = forumTable.getScanner(scan);

            System.out.println("前五条论坛数据：");
            for (Result result : scanner) {
                byte[] name = result.getValue(Bytes.toBytes("personal_info"), Bytes.toBytes("name"));
                byte[] feedback = result.getValue(Bytes.toBytes("feedback_details"), Bytes.toBytes("feedback"));
                byte[] date = result.getValue(Bytes.toBytes("meta_info"), Bytes.toBytes("date"));

                System.out.println("Name: " + Bytes.toString(name));
                System.out.println("Feedback: " + Bytes.toString(feedback));
                System.out.println("Date: " + Bytes.toString(date));
                System.out.println("---------------");
            }
            scanner.close();


            forumTable.close();


            admin.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

